package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.constant.RedisMessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.service.OrderMobileService;
import com.xrit.sunnyday.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

/**
 * 类名称: OrderMobileController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/29 下午 02:38
 * Version 1.0.0
 */
@RestController
@RequestMapping("/order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderMobileService orderService;


    @PostMapping("/submit")
    public Result submit(@RequestBody HashMap<String, String> orderInfo) {
        //得到jedis对象
        Jedis resource = jedisPool.getResource();
        //redis存入的验证码
        String redisInCode = resource.get(RedisMessageConstant.SENDTYPE_ORDER + "_" + orderInfo.get("telephone"));
        //判断redis里面有没有验证码
        if (null == redisInCode) {
            return new Result(false, "请先通过验证");
        }
        //判断验证码是否与redis里的验证码相同
        if (!redisInCode.equals(orderInfo.get("validateCode"))) {
            return new Result(false, "验证码不正确");
        }
        Order order = orderService.submit(orderInfo);
        System.out.println(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }
    @GetMapping("/findById")
    public Result findById(Integer id) throws ParseException {
      HashMap<String,String> map =  orderService.findById(id);
      //转换一下日期格式
        return new Result(true,MessageConstant.ORDER_SUCCESS,map);
    }
}
